<?php
/**
 * Admin_PrivilegesController
 *
 * @author Petr Laslo
 * @license MIT License
 * @package Knihovna\Admin\Controllers
 * @link https://code.google.com/p/knihovna/
 */
class Admin_PrivilegesController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $form = new Admin_Form_DeletePrivilege();

        if ($this->getRequest()->isPost() && (false !== $this->getRequest()->getPost('yes', false))) {
            try{
                $post = $this->getRequest()->getPost('id');
                $privilege = new Knihovna_Model_DbTable_Usersprivileges();
                $where = $privilege->getAdapter()->quoteInto('id = ?', $post);
                $privilege->delete($where);
                $this->_helper->flashMessenger('success|Oprávnění bylo úspěšně smazáno.');
                $this->_redirect('/admin/privileges/');
            }catch(Zend_Db_Exception $error){
                $this->_helper->flashMessenger('error|'.$error->getMessage());
                $this->_redirect('/admin/privileges/');
            }
        }elseif($this->getRequest()->isPost() && (false !== $this->getRequest()->getPost('no', false))){
            $this->_redirect('/admin/privileges/');
        }

        $this->view->form = $form;
        $privileges = new Knihovna_Model_DbTable_Usersprivileges();
        $select = $privileges->select()
            ->order('id');
        $privilegeslist = $privileges->fetchAll($select);
        $this->view->privileges = $privilegeslist;

        //TODO paginator

        //vypsání flashMessage
        $flashMessage = $this->_helper->flashMessenger;
        $this->view->message = $flashMessage->getMessages();

        //předvyplní oprávnění
        /*$role = new Knihovna_Model_DbTable_Usersrole();
        $zdroje = new Knihovna_Model_DbTable_Usersresources();
        $privileg ="login,logout,forgot,index,add,delete,edit";
        foreach($role->fetchAll() as $key=>$value)
        {
            foreach($zdroje->fetchAll() as $key2=>$value2)
            {
                    $resource = new Knihovna_Model_DbTable_Usersprivileges();
                    $data = array(
                        'resource' => $value2->nazev,
                        'role' => $value->nazev,
                        'privilege' => $privileg
                    );
                    $resource->insert($data);
            }
        }*/
    }

    /**
     * Přidání oprávnění
     *
     * @author Petr Laslo
     * @return void
     */
    public function addAction()
    {
        $form = new Admin_Form_AddPrivilege();

        if ($this->getRequest()->isPost() && (false !== $this->getRequest()->getPost('submit_add', false))) {
            if ($form->isValid($this->getRequest()->getPost())) {
                try{
                    $post = $form->getValues();

                    $resource = new Knihovna_Model_DbTable_Usersprivileges();
                    $data = array(
                        'resource' => $post['resource'],
                        'role' => $post['role'],
                        'privilege' => $post['privilege']
                    );
                    $resource->insert($data);

                    $this->_helper->flashMessenger('success|Oprávnění bylo úspěšně vytvořeno.');
                    $this->_redirect('/admin/privileges/');

                }catch(Exception $error){
                    $this->_helper->flashMessenger('error|'.$error->getMessage());
                    $this->_redirect('/admin/privileges/');
                }
            }
        }

        $this->view->form = $form;
    }

    /**
     * Editace oprávnění
     *
     * @author Petr Laslo
     * @return void
     */
    public function editAction()
    {
        $form = new Admin_Form_AddPrivilege();

        if ($this->getRequest()->isPost() && (false !== $this->getRequest()->getPost('submit_add', false))) {
            if ($form->isValid($this->getRequest()->getPost())) {
                try{
                    $post = $form->getValues();

                    $privilege = new Knihovna_Model_DbTable_Usersprivileges();
                    $data = array(
                        'resource' => $post['resource'],
                        'role' => $post['role'],
                        'privilege' => $post['privilege']
                    );

                    $where = $privilege->getAdapter()->quoteInto('id = ?', $this->_getParam('id'));
                    $privilege->update($data,$where);
                    $this->_helper->flashMessenger('success|Oprávnění bylo úspěšně editováno.');
                    $this->_redirect('/admin/privileges/');

                }catch(Exception $error){
                    $this->_helper->flashMessenger('error|'.$error->getMessage());
                    $this->_redirect('/admin/privileges/');
                }
            }
        }else{
            $id = $this->_getParam('id', 0);
            if ($id>0) {
                $privilege = new Knihovna_Model_DbTable_Usersprivileges();
                $where = $privilege->getAdapter()->quoteInto('id = ?', $id);
                $form->populate($privilege->getPrivilege($where));
            }
        }
        $this->view->form = $form;
    }
}

